home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util4 / dasaytim.lha / DaSayTime / DaSayTime.e < prev    next >
Text File  |  1995-08-27  |  9KB  |  403 lines

  1. OPT OSVERSION=37
  2.  
  3. /****
  4. **
  5. ** Product: DaSayTime.e
  6. **
  7. ** Syntax : DaSayTime <P=PATH> [S=SAMPLERATE] [V=VOLUME] [H=12H]
  8. **
  9. ** Author : Jørgen 'Da' Larsen (dapp@iesd.auc.dk)
  10. **            Carl Blochs Vej 20
  11. **            9000 Aalborg
  12. **            Denmark
  13. **
  14. ** Idea   : saytime - Jef Poskanzer
  15. **
  16. ** History: Version 0.1
  17. **                - First try!! Works :)
  18. **            Version 0.2
  19. **                - Added CONST instead of #define
  20. **                - Added version string
  21. **                - Added _better_ error handling
  22. **                - Added 12H option (18:00:00->06:00:00)        (SSP)
  23. **                - Added SAMPLERATE option (DEFAULT 8000)
  24. **                - Added real arg. handling
  25. **                - Added PAL/NTSC check
  26. **            Version 0.3
  27. **                - Bug when the clock was 00:__:__            (DA)
  28. **                  changed too 24:__:__
  29. **                - Added VOLUME option 
  30. **
  31. ** To do  : Figure out why
  32. **                IF arequest.length=4 THEN DeleteIORequest(arequest) /*WORKS*/
  33. **                IF arequest             THEN DeleteIORequest(arequest) /*CRASH*/
  34. **            Data:
  35. **                ioReq = CreateIORequest( ioReplyPort, size );
  36. **                ioReq - A pointer to the new IORequest block, or NULL.
  37. **                NULL:=NIL!!!!!
  38. **
  39. */
  40.  
  41. MODULE    'devices/audio',
  42.         'dos/datetime',
  43.         'dos/dos',
  44.         'dos/rdargs',
  45.         'exec/io',
  46.         'exec/memory',
  47.         'exec/ports',
  48.         'graphics/gfxbase'
  49.  
  50.  
  51. CONST PH_ONE = 1
  52. CONST PH_TWO = 2
  53. CONST PH_THREE = 3
  54. CONST PH_FOUR = 4
  55. CONST PH_FIVE = 5
  56. CONST PH_SIX = 6
  57. CONST PH_SEVEN = 7
  58. CONST PH_EIGHT = 8
  59. CONST PH_NINE = 9
  60. CONST PH_TEN = 10
  61. CONST PH_ELEVEN = 11
  62. CONST PH_TWELVE = 12
  63. CONST PH_THIRTEEN = 13
  64. CONST PH_FOURTEEN = 14
  65. CONST PH_FIFTEEN = 15
  66. CONST PH_SIXTEEN = 16
  67. CONST PH_SEVENTEEN = 17
  68. CONST PH_EIGHTEEN = 18
  69. CONST PH_NINETEEN = 19
  70. CONST PH_TWENTY = 20
  71. CONST PH_THIRTY = 21
  72. CONST PH_FORTY = 22
  73. CONST PH_FIFTY = 23
  74. CONST PH_THE_TIME_IS = 24
  75. CONST PH_OCLOCK = 25
  76. CONST PH_OH = 26
  77. CONST PH_EXACTLY = 27
  78. CONST PH_AND = 28
  79. CONST PH_SECOND = 29
  80. CONST PH_SECONDS = 30
  81.  
  82. CONST BUFFSIZE = 8192
  83.  
  84. ENUM ERR_ARG=1, ERR_VOL, ERR_INT, ERR_OPEN, ERR_MEM, ERR_PORT, ERR_IO
  85.  
  86. DEF path:PTR TO LONG, rate=8000, vol
  87.  
  88. PROC main() HANDLE
  89.  /* ReadArgs DEF's */
  90.  DEF readargs:PTR TO rdargs, args:PTR TO LONG
  91.  DEF eng
  92.  
  93.  /* Time DEF's */
  94.  DEF dt:datetime,ds:PTR TO datestamp
  95.  DEF hour,min,sec
  96.  
  97.  IF (readargs:=ReadArgs('P=PATH/A,S=SAMPLERATE,V=VOLUME,H=12H/S',args:=[NIL,NIL,NIL,NIL],NIL))=NIL THEN Throw(ERR_ARG,0)
  98.  
  99.  /* Arg Handling */
  100.  path:=String(StrLen(args[0]))
  101.  StrCopy(path,args[0],ALL)
  102.  
  103.  IF args[1]<>NIL        
  104.     rate:=Val(args[1])
  105.  ELSE
  106.     rate:=8000
  107.  ENDIF
  108.  
  109.  vol:=Val(args[2])
  110.  IF vol<>NIL
  111.      IF ( (vol<1) OR (vol>64) )
  112.         Throw(ERR_VOL,0)
  113.     ENDIF
  114.  ELSE
  115.     vol:=64
  116.  ENDIF
  117.  
  118.  eng:=args[3]
  119.  
  120.  /* Get the current time */
  121.  ds:=DateStamp(dt.stamp)
  122.  min,hour:=Mod(ds.minute,60)
  123.  sec:=ds.tick/50
  124.  
  125.  /* Say the time is */
  126.  sayphrase( PH_THE_TIME_IS );
  127.  
  128.  /* Say Hour */
  129.  IF eng=TRUE
  130.     IF ( hour=0 )
  131.         saynumber( 12, 0 )
  132.     ELSEIF ( hour > 12 )
  133.         saynumber( hour - 12, 0 )
  134.     ELSE
  135.         saynumber( hour, 0 )
  136.      ENDIF
  137.  ELSE
  138.     IF ( hour=0 )
  139.         saynumber( 24, 0 )
  140.     ELSE
  141.         saynumber( hour, 0 )
  142.     ENDIF
  143.  ENDIF
  144.  
  145.  /* Say min. */
  146.  IF ( min = 0 )
  147.     sayphrase ( PH_OCLOCK )
  148.  ELSE
  149.     saynumber( min, 1 )
  150.  ENDIF
  151.  
  152.  /* Say sec. */
  153.  IF ( sec = 0 )
  154.     sayphrase( PH_EXACTLY )
  155.  ELSE
  156.     sayphrase( PH_AND )
  157.     saynumber( sec, 0 )
  158.     IF ( sec = 1 )
  159.         sayphrase( PH_SECOND )
  160.     ELSE
  161.         sayphrase( PH_SECONDS )
  162.     ENDIF
  163.  ENDIF
  164.  
  165. EXCEPT
  166.     SELECT exception
  167.         CASE ERR_INT
  168.             PrintF( 'DaSayTime: Internal error - \s\n', exceptioninfo )
  169.         CASE ERR_VOL
  170.             PrintF( 'DaSayTime: Option VOLUME need a value from 1 to 64\n')
  171.         CASE ERR_ARG
  172.             PrintF('DaSayTime (c) 1995 Jørgen \aDa\a Larsen (Posse Pro. DK)\n'+
  173.                    'Syntax:  DaSayTime <P=PATH> [S=SAMPLERATE] [V=VOLUME] [H=12H]\n'+
  174.                    'Example: DaSayTime Work:music/DaSayTime/Sounds/\n')
  175.     ENDSELECT
  176. ENDPROC
  177.  
  178. PROC sayfile( filename )
  179.     DEF execstr[256]:STRING
  180.     StringF(execstr,'\s\s',path,filename)
  181.     play(execstr,rate)
  182. ENDPROC
  183.  
  184. PROC play(filename,rate) HANDLE
  185.     /* Audio DEF's */
  186.     DEF arequest:ioaudio, reply=NIL, ior:PTR TO io, adevice=1, mnode:PTR TO mn
  187.  
  188.     /* File DEF's */
  189.     DEF memoryblock=NIL, file, len
  190.  
  191.     /* NTSC/PAL check DEF's */
  192.     DEF gfxBase:PTR TO gfxbase, clock 
  193.  
  194.     /* Check if PAL or NTSC */        
  195.     gfxBase:=gfxbase
  196.     IF gfxBase.displayflags AND PAL
  197.         clock := 3546895;         /* PAL clock */
  198.     ELSE
  199.         clock := 3579545;         /* NTSC clock */
  200.     ENDIF
  201.  
  202.     IF (file:=Open(filename,MODE_OLDFILE))=NIL THEN Throw(ERR_OPEN,filename)
  203.  
  204.     IF (memoryblock:=AllocMem(BUFFSIZE,MEMF_CHIP))=NIL THEN Throw(ERR_MEM,BUFFSIZE)
  205.  
  206.     IF (reply:=CreateMsgPort())=NIL THEN Throw(ERR_PORT,0)
  207.  
  208.     IF (arequest:=CreateIORequest(reply, SIZEOF ioaudio))=NIL THEN Throw(ERR_IO,0)
  209.  
  210.     /* want to allocate and use any channel with OpenDev() */
  211.     arequest.data    := [1,2,4,8]:CHAR
  212.     arequest.length := 4                    /* This number is checks in EXECPT */
  213.     ior                := arequest
  214.     ior.command        := ADCMD_ALLOCATE
  215.  
  216.     IF (adevice:=OpenDevice('audio.device', 0, arequest, 0))<>NIL THEN Throw(ERR_OPEN,'audio.device')
  217.  
  218.     len:=Read(file,memoryblock,BUFFSIZE)
  219.     ior.flags        := ADIOF_PERVOL
  220.     ior.command        := CMD_WRITE
  221.     arequest.data    := memoryblock
  222.     arequest.length := len
  223.     arequest.volume := vol                    /* volume = MAX */
  224.     arequest.period := Div(clock,rate)        /* set frequency */
  225.     arequest.cycles := 1                    /* 1 cycle only */
  226.     mnode            := arequest
  227.     mnode.replyport := reply
  228.  
  229.     /* start audio device */
  230.     beginIO(arequest)
  231.  
  232.     /* clean up */
  233.     WaitPort(mnode.replyport)
  234.     GetMsg(mnode.replyport)
  235. EXCEPT DO
  236.     IF file THEN Close(file)
  237.     IF memoryblock THEN FreeMem(memoryblock,BUFFSIZE)
  238.     IF reply THEN DeleteMsgPort(reply)
  239.     IF arequest.length=4 THEN DeleteIORequest(arequest)
  240.     IF adevice=NIL THEN CloseDevice(arequest)
  241.     SELECT exception
  242.         CASE ERR_MEM
  243.             PrintF( 'DaSayTime: Could\an allocate \s bytes\n', exceptioninfo )
  244.         CASE ERR_PORT
  245.             PrintF( 'DaSayTime: Could\an Create Message Port\n')
  246.         CASE ERR_IO
  247.             PrintF( 'DaSayTime: Could\an Create IO port\n')
  248.         CASE ERR_OPEN
  249.             PrintF( 'DaSayTime: Could\an open \s\n', exceptioninfo )
  250.     ENDSELECT
  251. ENDPROC
  252.  
  253. PROC beginIO(arequest)
  254.     MOVE.L    arequest,A1
  255.     MOVE.L    A6,-(A7)
  256.     MOVE.L    $14(A1),A6
  257.     JSR -$1E(A6)
  258.     MOVE.L    (A7)+,A6
  259. ENDPROC
  260.  
  261. PROC saydigit( n )
  262.     SELECT n
  263.         CASE 1
  264.             sayphrase( PH_ONE )
  265.         CASE 2
  266.             sayphrase( PH_TWO )
  267.         CASE 3
  268.             sayphrase( PH_THREE )
  269.         CASE 4
  270.             sayphrase( PH_FOUR )
  271.         CASE 5
  272.             sayphrase( PH_FIVE )
  273.         CASE 6
  274.             sayphrase( PH_SIX )
  275.         CASE 7
  276.             sayphrase( PH_SEVEN )
  277.         CASE 8
  278.             sayphrase( PH_EIGHT )
  279.         CASE 9
  280.             sayphrase( PH_NINE )
  281.         DEFAULT
  282.             Throw(ERR_INT,'saydigit()')
  283.     ENDSELECT
  284. ENDPROC
  285.  
  286. PROC sayphrase( phrase )
  287.     SELECT phrase
  288.         CASE PH_ONE
  289.             sayfile( '1.iff' )
  290.         CASE PH_TWO
  291.             sayfile( '2.iff' )
  292.         CASE PH_THREE
  293.             sayfile( '3.iff' )
  294.         CASE PH_FOUR
  295.             sayfile( '4.iff' )
  296.         CASE PH_FIVE
  297.             sayfile( '5.iff' )
  298.         CASE PH_SIX
  299.             sayfile( '6.iff' )
  300.         CASE PH_SEVEN
  301.             sayfile( '7.iff' )
  302.         CASE PH_EIGHT
  303.             sayfile( '8.iff' )
  304.         CASE PH_NINE
  305.             sayfile( '9.iff' )
  306.         CASE PH_TEN
  307.             sayfile( '10.iff' )
  308.         CASE PH_ELEVEN
  309.             sayfile( '11.iff' )
  310.         CASE PH_TWELVE
  311.             sayfile( '12.iff' )
  312.         CASE PH_THIRTEEN
  313.             sayfile( '13.iff' )
  314.         CASE PH_FOURTEEN
  315.             sayfile( '14.iff' )
  316.         CASE PH_FIFTEEN
  317.             sayfile( '15.iff' )
  318.         CASE PH_SIXTEEN
  319.             sayfile( '16.iff' )
  320.         CASE PH_SEVENTEEN
  321.             sayfile( '17.iff' )
  322.         CASE PH_EIGHTEEN
  323.             sayfile( '18.iff' )
  324.         CASE PH_NINETEEN
  325.             sayfile( '19.iff' )
  326.         CASE PH_TWENTY
  327.             sayfile( '20.iff' )
  328.         CASE PH_THIRTY
  329.             sayfile( '30.iff' )
  330.         CASE PH_FORTY
  331.             sayfile( '40.iff' )
  332.         CASE PH_FIFTY
  333.             sayfile( '50.iff' )
  334.         CASE PH_THE_TIME_IS
  335.             sayfile( 'the_time_is.iff' )
  336.         CASE PH_OCLOCK
  337.             sayfile( 'oclock.iff' )
  338.         CASE PH_OH
  339.             sayfile( 'oh.iff' )
  340.         CASE PH_EXACTLY
  341.             sayfile( 'exactly.iff' )
  342.         CASE PH_AND
  343.             sayfile( 'and.iff' )
  344.         CASE PH_SECOND
  345.             sayfile( 'second.iff' )
  346.         CASE PH_SECONDS
  347.             sayfile( 'seconds.iff' )
  348.         DEFAULT
  349.             Throw(ERR_INT,'sayphrase()')
  350.     ENDSELECT
  351. ENDPROC
  352.  
  353. PROC saynumber( n, leadingzero )
  354.     DEF ones, tens
  355.     ones,tens:=Mod(n,10)
  356.     SELECT tens
  357.         CASE 0
  358.             IF ( leadingzero ) THEN sayphrase( PH_OH )
  359.             saydigit( ones )
  360.         CASE 1
  361.             SELECT ones
  362.                 CASE 0
  363.                     sayphrase( PH_TEN )
  364.                 CASE 1
  365.                     sayphrase( PH_ELEVEN )
  366.                 CASE 2
  367.                     sayphrase( PH_TWELVE )
  368.                 CASE 3
  369.                     sayphrase( PH_THIRTEEN )
  370.                 CASE 4
  371.                     sayphrase( PH_FOURTEEN )
  372.                 CASE 5
  373.                     sayphrase( PH_FIFTEEN )
  374.                 CASE 6
  375.                     sayphrase( PH_SIXTEEN )
  376.                 CASE 7
  377.                     sayphrase( PH_SEVENTEEN )
  378.                 CASE 8
  379.                     sayphrase( PH_EIGHTEEN )
  380.                 CASE 9
  381.                     sayphrase( PH_NINETEEN )
  382.                 DEFAULT
  383.                     Raise()
  384.             ENDSELECT
  385.         CASE 2
  386.             sayphrase( PH_TWENTY )
  387.             IF ( ones<>0 ) THEN saydigit( ones )
  388.         CASE 3
  389.             sayphrase( PH_THIRTY )
  390.             IF ( ones<>0 ) THEN saydigit( ones )
  391.         CASE 4
  392.             sayphrase( PH_FORTY )
  393.             IF ( ones<>0 ) THEN saydigit( ones )
  394.         CASE 5
  395.             sayphrase( PH_FIFTY )
  396.             IF ( ones<>0 ) THEN saydigit( ones )
  397.         DEFAULT
  398.             Throw(ERR_INT,'saynumber()')
  399.     ENDSELECT
  400. ENDPROC
  401.  
  402. CHAR '$VER: DaSayClock v0.3 (08.08.95) Jørgen ''Da'' Larsen',0
  403.